/*
 * @Descripttion: element 提示框
 * @Define: main.js 中 import DonMessage from './common/scripts/message'; Vue.prototype.$message = DonMessage;
 * @Use: 全局引入：this.$message.success("ceshi"); 局部引入： DonMessage.success("成功");
 * @Author: yeming
 * @Date: 2018-06-09 10:17:28
 */
import {
  Message
} from 'element-ui';

// 为了实现 Class 的私有属性
const showMessage = Symbol('showMessage');
const typeString = "[object String]";
const typeObject = "[object Object]";

/**
 *  重写 ElementUI 的 Message
 *  single默认值true，因为项目需求，默认只弹出一个，可以根据实际需要设置
 */

class DonMessage {
  success(options, single = true) {
    this[showMessage]('success', options, single);
  }
  warning(options, single = true) {
    this[showMessage]('warning', options, single);
  }
  info(options, single = true) {
    this[showMessage]('info', options, single);
  }
  error(options, single = true) {
    this[showMessage]('error', options, single);
  }
  close() {
    Message.closeAll();
  }

  [showMessage](type, options, single) {
    let _options = {};
    let typeOptions = Object.prototype.toString.call(options);
    switch (typeOptions) {
      case typeObject:
        _options = options;
        break;
      case typeString:
        _options = {
          message: options,
          duration: 2000,
          offset: 1
        };
        break;
    }
    if (single) {
      // 判断是否已存在Message
      if (document.getElementsByClassName('el-message').length === 0) {
        // Message.closeAll();
        Message[type](_options);
      }
    } else {
      Message[type](_options);
    }
  }
};
export default new DonMessage();
